package com.csi.service.impl;

import cn.hutool.core.text.StrBuilder;
import com.csi.domain.FacilityInfo;
import com.csi.domain.Room;
import com.csi.domain.RoomType;
import com.csi.domain.RoomtypeFacility;
import com.csi.mapper.HotelMapper;
import com.csi.mapper.RoomTypeMapper;
import com.csi.service.RoomTypeService;
import com.csi.utils.OssUtil;
import com.csi.utils.ReturnResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RoomTypeServiceImpl implements RoomTypeService {

    @Autowired
    private RoomTypeMapper roomTypeMapper;

    @Autowired
    private HotelMapper hotelMapper;

    /**
     * 添加房间类型
     *
     * @param roomType       房间类型
     * @param multipartFiles
     * @return 执行条数
     */
    @Override
    @Transactional
    public int save(RoomType roomType, MultipartFile[] multipartFiles) {
        List<String> filesHrefs = new ArrayList<>();
        if (multipartFiles!=null && multipartFiles.length>0){
            // 遍历文件列表，逐个上传
            for (MultipartFile uploadFile : multipartFiles) {
                // 调用 OssUtil 上传文件
                try {
                    filesHrefs.add(OssUtil.upload(uploadFile.getOriginalFilename(), uploadFile.getInputStream(), "hotels/roomtype"));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        }
        StrBuilder bu = new StrBuilder();
        if (filesHrefs.size() > 1){
            for (int i = 0; i < filesHrefs.size(); i++) {
                bu.append(filesHrefs.get(i));
                if (i < filesHrefs.size() - 1) { // 最后一个文件不加分号
                    bu.append(";");
                }
            }
        }else {
            bu.append(filesHrefs.get(0));
        }

        roomType.setRoomImage(bu.toString());
        //返回自增主键
        //新增房型
        int savedId = roomTypeMapper.save(roomType);
        int saved3 = roomTypeMapper.insertAudit(roomType);
        int num = roomType.getRoomNum();

        //新增房间
        List<Room> rooms = new ArrayList<>();
        for(int i=0;i<num;i++){
            Room room = new Room();
            room.setRoomStatus(ReturnResults.INITIAL_ROOM_STATUS);
            room.setTypeId(roomType.getId());
            rooms.add(room);
        }
        int saved = roomTypeMapper.saveRooms(rooms);

        //新增房间设施
        List<RoomtypeFacility> roomtypeFacilities = new ArrayList<>();
        for (int root:roomType.getRoots()){
            RoomtypeFacility roomtypeFacility = new RoomtypeFacility();
            roomtypeFacility.setRoomtypeId(roomType.getId());
            roomtypeFacility.setRoomtypeFacilityId(root);
            roomtypeFacilities.add(roomtypeFacility);
        }
        int saved2 = roomTypeMapper.saveRoomTypeFacilities(roomtypeFacilities);


        return saved + savedId+saved2 + saved3; //增加房间数量条数+增加房型数量条数
    }

    /**
     * 更新房间类型
     * @param roomType 房间类型
     * @return 执行条数
     */
    @Override
    @Transactional
    public int update(RoomType roomType,MultipartFile[] multipartFiles) {
        List<String> filesHrefs = new ArrayList<>();
        if (multipartFiles!=null && multipartFiles.length>0){
            // 遍历文件列表，逐个上传
            for (MultipartFile uploadFile : multipartFiles) {
                // 调用 OssUtil 上传文件
                try {
                    filesHrefs.add(OssUtil.upload(uploadFile.getOriginalFilename(), uploadFile.getInputStream(), "hotels/roomtype"));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        }
        StrBuilder bu = new StrBuilder();
        if (filesHrefs.size() > 1){
            for (int i = 0; i < filesHrefs.size(); i++) {
                bu.append(filesHrefs.get(i));
                if (i < filesHrefs.size() - 1) { // 最后一个文件不加分号
                    bu.append(";");
                }
            }
        }else {
            bu.append(filesHrefs.get(0));
        }

        roomType.setRoomImage(bu.toString());


        int updated = roomTypeMapper.update(roomType); //更新房型
        int deleted = roomTypeMapper.delete(roomType.getId());  //根据id删除房间设施
        List<RoomtypeFacility> roomtypeFacilities = new ArrayList<>();
        for (int root:roomType.getRoots()){
            RoomtypeFacility roomtypeFacility = new RoomtypeFacility();
            roomtypeFacility.setRoomtypeId(roomType.getId());
            roomtypeFacility.setRoomtypeFacilityId(root);
            roomtypeFacilities.add(roomtypeFacility);
        }
        int saved2 = roomTypeMapper.saveRoomTypeFacilities(roomtypeFacilities);

        return saved2 + deleted + updated;
    }


    /**
     * 根据id查找房型
     * @param roomtypeId 房型id
     * @return 请求结果
     */
    @Override
    @Transactional
    public RoomType findById(long roomtypeId) {
        RoomType roomType =  roomTypeMapper.findById(roomtypeId);
        Integer[] roots =  roomTypeMapper.findRoots(roomtypeId);
        roomType.setRoots(roots);
//        List<FacilityInfo> facilityInfos = roomTypeMapper.findFacilityInfo(roomtypeId);
//        roomType.setFacilityInfos(facilityInfos);
        return roomType;
    }

    /**
     * 根据酒店id查询房型
     * @param hotelId 酒店id
     * @return 请求结果
     */
    @Override
    public List<RoomType> findAll(long hotelId) {
        return roomTypeMapper.findAll(hotelId);
    }

    /**
     * 线下入住 默认点击后会更改一个房间的状态
     * @param roomtypeId 房型id
     * @return 请求结果
     */
    @Transactional
    @Override
    public int checkInOffline(long roomtypeId) {
        int updated =  roomTypeMapper.checkInOffline(roomtypeId,ReturnResults.CHECK_INFO_OFFICE_ROOM_STATUS);
        int deleted = roomTypeMapper.updateRoomtypeNum(roomtypeId);

        return updated + deleted;
    }

    @Override
    public List<FacilityInfo> findFacility(long hotelId) {
        return roomTypeMapper.findFacility(hotelId);
    }

    @Override
    public Map<String, Object> findRooms(long hotelId) {
        return roomTypeMapper.findRooms(hotelId);
    }


}
